﻿using MathWorks.MATLAB.NET.Arrays;
using myMatlabDll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12.Controls
{
    public class MyMatlab2
    {
        public static List<double> FFTre(List<double> y)
        {
            int DN = y.Count;

            myMatlabClass matlab = new myMatlabClass();

            MWNumericArray dd = y.ToArray();
            MWArray arr = dd;



            MWArray res = matlab.myFFT(arr);

            List<double> fres = new List<double>();

            int rn = res.NumberOfElements;

            //MWNumericArray arr2 = (MWNumericArray)res;

            double[,] dd2 = (double[,])res.ToArray();

            //for(int i=0;i< rn;i++)
            //{
            //    fres.Add(dd2[0, i]);
            //}

            List<double> fcom = new List<double>();
            for (int i = 0; i < rn; i++)
            {
                fcom.Add(dd2[0, i]);
            }

            return fcom;
        }

        public static MData FFT(List<double> y, int sampleFre)
        {
            MData res = new MData();

            if (y.Count == 0)
            {
                return res;
            }

            int DL = y.Count;

            double sp = sampleFre * 1.0 / DL;

            List<double> cp1 = FFTre(y);

            int FN = cp1.Count / 2;

            for (int i = 0; i < FN; i++)
            {
                double t1 = cp1[i];

                double t2 = t1 * 1.0 / DL;

                if (i > 0)
                {
                    t2 = 2 * t2;
                }

                res.YValues.Add(t2);

                double cf = i * sp;

                res.XValues.Add(cf);
            }

            return res;
        }

        public static MData Filter(MData dd, int fre, double bandwidth, int samplefre, int n)
        {
            MData res = new MData();

            myMatlabClass matlab = new myMatlabClass();

            MWNumericArray narr = dd.YValues.ToArray();
            MWArray arr = narr;

            MWArray filtres = matlab.myFilterFir(arr, fre - bandwidth / 2, fre + bandwidth / 2, samplefre, n);

            res.XValues = dd.XValues;

            double[,] dd2 = (double[,])filtres.ToArray();

            int rn = filtres.NumberOfElements;

            for (int i = 0; i < rn; i++)
            {
                res.YValues.Add(dd2[0, i]);
            }

            return res;

        }

        public static MData Hilbert(MData dd)
        {
            MData res = new MData();

            myMatlabClass matlab = new myMatlabClass();

            MWNumericArray narr = dd.YValues.ToArray();
            MWArray arr = narr;

            MWArray filtres = matlab.myHilbert(arr);

            //res.XValues = dd.XValues;

            double[,] dd2 = (double[,])filtres.ToArray();

            int rn = filtres.NumberOfElements;

            for (int i = 0; i < rn; i++)
            {
                res.YValues.Add(dd2[0, i]);
                res.XValues.Add(i);
            }

            return res;

        }

        public static void Init()
        {
            List<double> tmp = new List<double>() { 1, 2, 3, 4, 5 };
            FFT(tmp, 1);
        }
    }
}
